package br.usp.pcs.lti.planning;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;

import rddl.EvalException;
import rddl.RDDL;
import rddl.parser.parser;
import rddl.viz.StateViz;

public class PiHullHelper {

    /** Nome do arquivo RDDL */
    private String rddlFile = "resources/traffic_noInt2.rddl";

    /** Nome da instancia */
    private String instanceName = "vehicles7cross1";

    private RDDL rddl;
    private MASimulator simulator;
    private StateViz viz;
    private Random rand_seed = new Random();

    public PiHullHelper() throws Exception {
        File f = new File(this.rddlFile);
        if (!f.exists()) {
            throw new FileNotFoundException("Arquivo " + this.rddlFile + " nao encontrado");
        }
        this.rddl = new RDDL(parser.parse(f));
        this.simulator = new MASimulator(this.rddl, this.instanceName);
        this.viz = new TrafficDisplay();
    }

    public double[] findBestAttribute(double[] weightR, int policyID) throws EvalException, IOException {

        Planner planner = new Planner();
        SPUDDPolicy spuddPolicy = planner.learnSPUDDPolicy(this.rddl, this.instanceName);

        this.simulator.setRewardFunction(weightR);

        MAResult result = this.simulator.run(spuddPolicy, this.viz, this.rand_seed.nextLong());
        System.out.println("==> " + result);

        this.viz.close();
        Utils.serializeObject(spuddPolicy, "resources/policy" + policyID + ".ser");

        return result._rewardsPerAttribute;
    }

    public static void main(String[] args) throws Exception {
        PiHullHelper helper = new PiHullHelper();
        helper.findBestAttribute(new double[] { -1.0, -2.0 }, 1);
    }

    public String getRddlFile() {
        return this.rddlFile;
    }

    public void setRddlFile(String rddlFile) {
        this.rddlFile = rddlFile;
    }

    public String getInstanceName() {
        return this.instanceName;
    }

    public void setInstanceName(String instanceName) {
        this.instanceName = instanceName;
    }

}
